#ifndef _CONSOLE_PARSER_H
#define _CONSOLE_PARSER_H

template<typename T>
class DLL_EXPORT ConsoleParser
{
	T* obj;
	typedef void (T::*hndl)(char*);
	struct Handlers
	{
		const char* name;
		hndl proc;
	};
	list<Handlers*> m_handlers;
public:
	ConsoleParser(T* inst) : obj(inst)
	{
	}

	void AddHandler(const char* command, hndl proc)
	{
		Handlers* h = new Handlers;
		h->name = command;
		h->proc = proc;
		m_handlers.push_back(h);
	}
	
	void ParseLine(char* line)
	{
		if(!line)
			return;
		char* cmd = strtok(line, " ");
		if(!cmd)
			return;
		char* remaining = strtok(line, "\0");
		list<Handlers*>::iterator itr = m_handlers.begin();
		list<Handlers*>::iterator end = m_handlers.end();

		for( ; itr != end; ++itr)
		{
			if(!stricmp((*itr)->name, cmd))
			{
				(obj->*(*itr)->proc)(remaining);
				return;
			}
		}

		Log->Notice("Console", "Could not find command '%s'", line);
	}
};

#endif